var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chart_left:0,
    cur_index:0,
    rad_index:0,
    ChargeList:[],
    ChargeList0:[],
    ChargeList1:[],
    orderList: [],
    page: 1,
    size: 1000,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1,

    years:[],
    year:'2021',
    year_index:0,
    months:[],
    month:'',
    month_index:0,
    sorts:['全部账单类型','支出','充值'],
    sort_index:0,
    arr_sorts:[{id:-1,text:'全部账单类型'},{id:0,text:'支出'},{id:1,text:'充值'}]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var userInfo=wx.getStorageSync('userInfo');
    if(userInfo!="")
    {
      this.setData({userInfo:userInfo});
    }

    const date = new Date();
    var years = new Array()
    var months =new Array()

    for (let i = 2020; i <= date.getFullYear(); i++) 
    {
      years.push(i)
    }
    years.map(function(item,i){
      if(date.getFullYear()==item)
      {
        that.setData({year_index:i});
      }
    });

    for (let i = 1; i <= 12; i++) {
      months.push(i)
    }
    months.map(function(item,i){
      if((date.getMonth()+1)==item)
      {
        that.setData({month_index:i});
      }
    });

    this.setData({years:years});
    this.setData({months:months});
    this.setData({year:date.getFullYear()});
    this.setData({month:(date.getMonth()+1)});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo=this.data.userInfo;
    if(userInfo!="")
    {
      this.get_logs();
      this.get_logs2();
      this.get_logs3();
      this.get_charge();
      if(userInfo.ST==2||userInfo.ST==1)
      {
        
      }
      else
      {  
        /*      
        wx.showModal({
          title: '提示',
          content:'请先完善个人信息',
          success(res1) 
          {
            if (res1.confirm) 
            {
              wx.navigateTo({url: '/pages/auth/register/register'});
            }
            else if (res1.cancel) 
            {
              wx.switchTab({ url: '/pages/index/index' });
            }
          }
        })
        */
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  swtch_tab:function(e){
    console.log(e.currentTarget.dataset.index);
    this.setData({cur_index:e.currentTarget.dataset.index});
  },
  get_charge:function(){
    var that=this;
    var userid="";
    if(that.data.userInfo!="")
    {
      userid=that.data.userInfo.Id;
    }
    else
    {
      wx.showToast({title: '请先登录'});
      return;
    }
    util.request(api.GetUsersChargeLeft, {UserID:userid},'GET').then(function (res) {
      if (res.Status === 100) 
      {
         that.setData({chart_left:res.Data});
      }
    });
  },
  get_logs:function(){
    let that = this;

    if (that.data.totalPages <= that.data.page - 1) 
    {
      that.setData({nomore: true});
      return;
    }
    var userid="";
    if(that.data.userInfo!="")
    {
      userid=that.data.userInfo.Id;
    }
    else
    {
      wx.showToast({title: '请先登录'});
      return;
    }

    //会员等级费用
    util.request(api.GetUsersPayInfoList, {UserID:userid, Page: that.data.page, PageSize: that.data.size},'GET').then(function (res) {
      if (res.Status === 100) 
      {
        for(var i=0;i<res.Data.Models.length;i++)
        {
          res.Data.Models[i].transaction_date=util.shortDate(res.Data.Models[i].transaction_date);
          res.Data.Models[i].expire_date=util.shortDate(res.Data.Models[i].expire_date);
        }
        that.setData({
          orderList: that.data.orderList.concat(res.Data.Models),
          page: res.Data.CurrentPage + 1,
          totalPages: res.Data.TotalPage
        });
      }
      else
      {
        wx.showToast({title: '没有数据'});
      }
    });
  },

  get_logs2:function(){
    let that = this;

    if (that.data.totalPages <= that.data.page - 1) 
    {
      that.setData({nomore: true});
      return;
    }
    var userid="";
    if(that.data.userInfo!="")
    {
      userid=that.data.userInfo.Id;
    }
    else
    {
      wx.showToast({title: '请先登录'});
      return;
    }
    var pd={
      keyword:'',
      ChargeSort:-2,
      UserID:userid, 
      Year:'',
      Month:'',
      Page: that.data.page, PageSize: that.data.size
    };
    util.request(api.GetUsersChargeList,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      {
        for(var i=0;i<res.Data.Models.length;i++)
        {
          res.Data.Models[i].CreateDate=util.formatTime(res.Data.Models[i].CreateDate);
        }
        var arr1=new Array();
        var arr0=new Array();
        res.Data.Models.map(function(item){
            if(item.ChargeSort==1)
            {
              arr1.push(item);
            }
            else
            {
              arr0.push(item);
            }
        }); 
        that.setData({ChargeList1:arr1});
      }
      else
      {
        wx.showToast({title: '没有数据'});
      }
    });
  },
  get_logs3:function(){
    let that = this;
    console.log('#1');
    if (that.data.totalPages <= that.data.page - 1) 
    {
      that.setData({nomore: true});
      return;
    }
    var userid="";
    if(that.data.userInfo!="")
    {
      userid=that.data.userInfo.Id;
    }
    else
    {
      wx.showToast({title: '请先登录'});
      return;
    }
    var pd={
      keyword:'',
      ChargeSort:-2,
      UserID:userid, 
      Year:that.data.year,
      Month:that.data.month,
      Page: that.data.page, PageSize: that.data.size
    };
    util.request(api.GetUsersChargeList,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      {
        var chart1=0;
        var chart0=0;
        for(var i=0;i<res.Data.Models.length;i++)
        {
          if(res.Data.Models[i].ChargeSort==1)
          {
            chart1+=res.Data.Models[i].ChargeTotal;
          }
          if(res.Data.Models[i].ChargeSort==0)
          {
            chart0+=res.Data.Models[i].ChargeTotal;
          }
          res.Data.Models[i].CreateDate=util.formatTime(res.Data.Models[i].CreateDate);
        }
        that.setData({chart1:chart1});
        that.setData({chart0:chart0});
        that.setData({
          ChargeList: that.data.ChargeList.concat(res.Data.Models),
          page: res.Data.CurrentPage + 1,
          totalPages: res.Data.TotalPage
        });
         
      }
      else
      {
        that.setData({chart1:0});
        that.setData({chart0:0});
        wx.showToast({title: '没有数据'});
      }
    });
  },
  radioChange(e) {
    this.setData({rad_index:e.detail.value});
  },
  bindPickerChange1(e) { 
    var that=this;
    that.setData({year_index: e.detail.value})
    var years=this.data.years;
    that.setData({year:years[e.detail.value]})
    that.setData({
      ChargeList: [],
      page:  1,
      totalPages:1
    });

    that.get_logs3();
  },
  bindPickerChange2(e) 
  { 
    this.setData({month_index: e.detail.value});
    this.setData({month:this.data.months[e.detail.value]});
    this.setData({
      ChargeList: [],
      page:  1,
      totalPages:1
    });
    this.get_logs3();
  },
  bindPickerChange3(e) { 
    this.setData({sort_index: e.detail.value});
    this.setData({
      ChargeList: [],
      page:  1,
      totalPages:1
    });
    this.get_logs3();
  },
})